package com.tcgroup.phone.base_core.framework.mvvm.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tcgroup.phone.base_core.event.event_livedata.SingleLiveEvent;
import com.tcgroup.phone.base_core.framework.mvvm.netregister.BaseNetRegister;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/18.
 * 包名: com.tcgroup.phone.base_core.framework.mvvm.viewmodel
 * 类名: OSAndroidMvvm
 */
public abstract class BaseRefreshViewModel<T, M extends BaseNetRegister>
        extends BaseViewModel<M> implements OnRefreshListener, OnLoadMoreListener {

    protected ObservableArrayList<T> mList = new ObservableArrayList<>();
    public ObservableField<Boolean> enableRefresh = new ObservableField<>(true);
    public ObservableField<Boolean> enableLoadMore = new ObservableField<>(true);
    public ObservableBoolean isFinishRefresh = new ObservableBoolean();
    public ObservableBoolean isFinishLoadMore = new ObservableBoolean();
    public ObservableField<Boolean> orientation = new ObservableField( false);
    public ObservableField<Integer> spanCount = new ObservableField( 2);



    public BaseRefreshViewModel(@NonNull Application application, M netRegister) {
        super(application, netRegister);
    }

    @Override
    public void initViewByReFresh() {
        super.initViewByReFresh();
    }

    public ObservableArrayList<T> getList() {
        return mList;
    }

    protected BaseRefreshViewModel.UIChangeRefreshLiveData mUIChangeRefreshLiveData;

    public BaseRefreshViewModel.UIChangeRefreshLiveData getUCRefresh() {
        if (mUIChangeRefreshLiveData == null) {
            mUIChangeRefreshLiveData = new BaseRefreshViewModel.UIChangeRefreshLiveData();
        }
        return mUIChangeRefreshLiveData;
    }

    public final class UIChangeRefreshLiveData extends SingleLiveEvent{
        private SingleLiveEvent<Boolean> mStopRefresLiveEvent;
        private SingleLiveEvent<Void> mStopLoadMoreLiveEvent;
        private SingleLiveEvent<Void> mLoadMoreWithNoMoreLiveEvent;
        private SingleLiveEvent<Void> mAutoRefresLiveEvent;
        private SingleLiveEvent<Void> mAutoLoadMoreLiveEvent;
        public SingleLiveEvent<Boolean> getRefresLiveEvent() {
            return mStopRefresLiveEvent = createLiveData(mStopRefresLiveEvent);
        }
        public SingleLiveEvent<Void> getLoadMoreLiveEvent() {
            return mStopLoadMoreLiveEvent = createLiveData(mStopLoadMoreLiveEvent);
        }
        public SingleLiveEvent<Void> getLoadMoreWithNoMoreLiveEvent(){
            return mLoadMoreWithNoMoreLiveEvent = createLiveData(mLoadMoreWithNoMoreLiveEvent);
        }
        public SingleLiveEvent<Void> getAutoRefresLiveEvent(){
            return mAutoRefresLiveEvent = createLiveData(mAutoRefresLiveEvent);
        }
        public SingleLiveEvent<Void> getAutoLoadMoreLiveEvent(){
            return mAutoLoadMoreLiveEvent = createLiveData(mAutoLoadMoreLiveEvent);
        }
    }


    public void postRefreshEvent(boolean flag){
        if(mUIChangeRefreshLiveData != null){
            mUIChangeRefreshLiveData.getRefresLiveEvent().setValue(flag);
        }
    }

    public void postLoadMoreEvent(boolean flag){
        if(mUIChangeRefreshLiveData != null){
            mUIChangeRefreshLiveData.getLoadMoreLiveEvent().setValue(flag);
        }
    }

    public void postLoadMoreWithNoMoreEvent(){
        if(mUIChangeRefreshLiveData != null) {
            mUIChangeRefreshLiveData.getLoadMoreLiveEvent().call();
        }
    }

    public void postLoadAutoRefresMoreEvent(){
        if(mUIChangeRefreshLiveData != null) {
            mUIChangeRefreshLiveData.getAutoRefresLiveEvent().call();
        }
    }

    public void postLoadLoadMoreLiveMoreEvent(){
        if(mUIChangeRefreshLiveData != null) {
            mUIChangeRefreshLiveData.getAutoLoadMoreLiveEvent().call();
        }
    }


    //下拉刷新
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        loadMore();
    }

    public abstract void refreshData();

    public abstract void loadMore();


}
